clear;close all;clc;

% we read in our single-source recordings to create our own models at 4
% frequencies.

% freqs = [200 300 500 600 800 1000 1200 1500 2000 3000];
freqs = 100:50:3000;
% nturns=[0.1 0.4 0.8 0.7 0.8 1.2 1.3 1.4 1.8 2.2];
% rad=0.2;
rad=0.1;
Norder=2;
thre = 0.0;


% tdur=0.02; % formal
% tdur=0.04; % formal
% tdur=0.08; % formal
% tdur=1; % formal

% freqs= [291 600 1500 3000]; %just a random frequency
% freqs= [300 600 850 900 1000 2000]; %just a random frequency
% freqs= [ 850 900 1000]; %just a random frequency
% nturns = [0.98 1.09 1.13];
% freqs= [  910]; %just a random frequency
% nturns = [ 1.13];
fs = 44100;
% whos
plot_each =1;

projection1 = 0; % neariest center
Num_centers = 1;
% Proto_Num = 1.2;
Proto_Num = 21;
% Proto_Num = 22;

if Proto_Num ==21
    onset = 4;
    period = 20;
else
    onset = 5;
    period = 10;
end

% Angles = -90:10:90;
Angles = [ 60];
sides = {'Left','Right'};


f2=figure('position',[100 70 1800 800]);

for ifreql = 1:length(freqs)
    % for ifreql = 1
    % f1=figure('position',[100 70 1600 700]);
    freq = freqs(ifreql)
    tdur = 40/freq;
    for iangle = 1:length(Angles)
        % cc = 0;
        Angle = Angles(iangle);
        PString = num2str(Proto_Num);
        if Angle < 0
            % eval(['load All_sound_recordings\Protocol_' PString(1) '\Protocol_' PString '_' sides{1} '_'   num2str(abs(Angles(iangle))) 'degree.mat']);
            eval(['load All_sound_recordings\Protocol_' PString '\Phoneme_' PString '_' sides{1} '_'   num2str(abs(Angles(iangle))) 'degree.mat']);
        elseif Angle ==0
            % eval(['load All_sound_recordings\Protocol_' PString(1) '\Protocol_' PString '_' sides{1} '_'   num2str(abs(Angles(iangle))) 'degree.mat']);
            eval(['load All_sound_recordings\Protocol_' PString '\Phoneme_' PString '_'  num2str(Angles(iangle)) 'degree.mat']);
        else
            % eval(['load All_sound_recordings\Protocol_' PString(1) '\Protocol_' PString '_' sides{1} '_'   num2str(abs(Angles(iangle))) 'degree.mat']);
            eval(['load All_sound_recordings\Protocol_' PString '\Phoneme_' PString '_' sides{2} '_'   num2str(Angles(iangle)) 'degree.mat']);
        end

        beginP = (ifreql-1).*period.*fs+1;
        endP = ifreql.*period.*fs;


        if ifreql==1 & iangle==1
            figure
            NL = length(recorded_sound(:,1));
            tsig = (1:NL)./fs;
            plot(tsig,recorded_sound(:,1))
            hold on

            for i=1:round(NL./fs/period)+1
                plot([onset onset]+period.*(i-1),[-1 1],'r');
            end
        end

        recorded_sound=recorded_sound(onset.*fs+beginP:onset.*fs+endP,:);
        left_sound = recorded_sound(:,1);
        right_sound = recorded_sound(:,2);
        [SL,SR,freq_pos,flag]=mySTFT(left_sound,right_sound,fs,thre,tdur);
        % normalization:
        % [SL1,SR1]=Normalize(SL,SR);
        % if Angle < 0
        %     L_or_R=1;
        % else
        L_or_R=2;
        % end
        SLR=Normalize_Both(SL,SR,L_or_R);
        [Y Ind] = min(abs(freq_pos - freq));
        X_P = [real(SLR(Ind,flag>0))' imag(SLR(Ind,flag>0))'];

        out = f_mycenter(X_P,rad);
        F_centers(ifreql,:) = [freq out.C1];

        if ifreql<29
            figure(f2)
            subplot(4,7,ifreql);
            plot(X_P(:,1),X_P(:,2),'k.');hold on;axis square
            s=plot(F_centers(ifreql,2),F_centers(ifreql,3),'go');hold on;set(s,'linewidth',2)
            set(gca,'Xtick',-1:0.5:1)
            set(gca,'Ytick',-1:0.5:1)
            if Angle < 0
                Pangle =['-' num2str(abs(Angles(iangle))) ];
            elseif Angle ==0
                Pangle =[num2str(abs(Angles(iangle))) ];
            else
                Pangle =['+' num2str(abs(Angles(iangle))) ];
            end
            title([num2str(freq) 'Hz;' Pangle]);
            axis([-1 1 -1 1])
        end
        % figure(f1)
        % subplot(2,5,ifreql)
        % s=text(F_centers(iangle,1),F_centers(iangle,2),Pangle);hold on;set(s,'fontsize',7)
        % hold on;
        % axis([-1 1 -1 1])
        % pause(0.01)
    end % angle

    hold on;
    plot(-1:1,[0,0,0],'k', 'LineWidth', 0.5);
    plot([0,0,0],-1:1,'k','LineWidth',0.5)
    set(gca,'fontsize',10)
    xlabel('Re','fontsize',12)
    ylabel('Im','fontsize',12)
    hold on

    % figure(f2)
    % subplot(2,5,ifreql)
    % plot(F_centers(:,1),F_centers(:,2),'r')
    % title([num2str(freq) ' Hz'])
    % axis([-1 1 -1 1]);axis square
    % pause(0.1)
    %==============================
    % out=f_get_spiral(F_centers,Norder,nturns(ifreql));
    % x_spiral=out.x_spiral;
    % y_spiral=out.y_spiral;
    % plot(x_spiral,y_spiral,'color',[0.5 0.5 0.5]) ; % nturns crossings, including end point
    % axis square

end % freq
F_centers
save Freq_centers_sinu_all_long  F_centers



